package com.briup.srms.project.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.srms.project.bean.EmployeeChannelProject;
import com.briup.srms.project.bean.EmployeeChannelProjectExample;
import com.briup.srms.project.dao.EmployeeChannelProjectMapper;
import com.briup.srms.project.dao.extend.EmployeeChannelProjectExtendMapper;
import com.briup.srms.project.service.IEmployeeChannelProjectService;
import com.briup.srms.project.vm.EmployeeChannelProjectVM;
import com.jacky.base.utils.CustomerException;

@Service
public class EmployeeChannelProjectServiceImpl implements IEmployeeChannelProjectService {
	
	@Autowired
	private EmployeeChannelProjectExtendMapper ecpExtendMapper;
	@Autowired
	private EmployeeChannelProjectMapper ecpMapper;

	@Override
	public List<EmployeeChannelProjectVM> query() {
		
		
		return ecpExtendMapper.select();
	}

	@Override
	public void assignment(Long[] channelIds,EmployeeChannelProject ecp) {
//		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
//		String period = simpleDateFormat.format(new Date());
		for(long channelId : channelIds) {
			// 查看当前渠道是否被人占用
			List<EmployeeChannelProject> list =  this.query(channelId, ecp.getPeriod());
			if(list.size()>0) {
				throw new CustomerException("选中的渠道已经分配！");
			}
			
			ecp.setChannelId(channelId);
			ecpMapper.insert(ecp);
		}
	}
	
	private List<EmployeeChannelProject> query(Long channelId,String period){
		EmployeeChannelProjectExample ecpExample = new EmployeeChannelProjectExample();
		EmployeeChannelProjectExample.Criteria criteria = ecpExample.createCriteria();
		if(channelId != null) {
			criteria.andChannelIdEqualTo(channelId);
		}
		if(period != null) {
			criteria.andPeriodEqualTo(period);
		}
		return ecpMapper.selectByExample(ecpExample);
		
		
	}
	
	
	
	

}
